package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.CustomerException;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;



@Service


public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    CategoryService categoryService;

    @Override
    @Transactional
    /**
     * 保存套餐信息及关联菜品
     */
    public void saveAndDish(SetmealDto setmealDto) {


        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();


        //给菜品表的套餐id赋值
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
        });

        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 分页查询套餐信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> page(Integer page, Integer pageSize, String name) {

        Page<Setmeal> page1 = new Page<>(page, pageSize);

        LambdaQueryWrapper<Setmeal> queryWrapper = Wrappers.lambdaQuery(Setmeal.class).like(StringUtils.isNotBlank(name), Setmeal::getName, name);
        this.page(page1, queryWrapper);


        Page<SetmealDto> page2 = new Page<>(page, pageSize);

        BeanUtils.copyProperties(page1,page2,"records");

        List<Setmeal> records = page1.getRecords();


/*        List<SetmealDto> list = Stream.of(records).map(setmeal -> {
            System.out.println(setmeal);
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryService.getById(setmeal.get);
            if(category!=null){
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());*/

        List<SetmealDto> list1 = page1.getRecords().stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            if (category != null) {
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());


        page2.setRecords(list1);
        return page2;
    }


    /**
     * 删除套餐信息及套餐-菜品表对应信息
     * @param ids
     */
    @Override
    public void deleteAndDish(Long[] ids) {

        //如果有未停售套餐则提示不能删除

        LambdaQueryWrapper<Setmeal> wrapper = Wrappers.lambdaQuery(Setmeal.class).in(Setmeal::getId,Arrays.asList(ids))
                .eq(Setmeal::getStatus, 1);

        int count = this.count(wrapper);
        if(count>0){
            throw new CustomerException("有未停售套餐,不能删除");
        }


        //根据主键删除套餐信息
        //Arrays.asList把数组变为list集合
        this.removeByIds(Arrays.asList(ids));


        //删除套餐-菜品表关联信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = Wrappers.lambdaQuery(SetmealDish.class).in(SetmealDish::getSetmealId,Arrays.asList(ids));

        setmealDishService.remove(queryWrapper);




    }
}
